package com.bjc.config;



import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;

import java.util.Arrays;

/**
 * @program: airspringaop
 * @description:
 * @author: bjc
 * @create: 2020-08-30 15:31
 */
@Aspect
@Component
public class LoggerAspect {

    @Autowired
    private TransactionUtils transManager;

    TransactionStatus status;

    @Pointcut(value = "execution(public int com.bjc.controller.UserController.add(..))")
    public void webLog(){
    }

    @AfterThrowing(value = "webLog()")
    public void afterThrowing() {
        System.out.println("异常增强");
        transManager.rollback(status);
    }

    @Around(value = "webLog()")
    public void around(ProceedingJoinPoint joinPoint ) throws Throwable {
        System.out.println("环绕通知开始");
        //获取事务
        transManager.begin();
        joinPoint.proceed();
        //结束事务
        System.out.println("环绕通知结束");
        transManager.commit(status);
    }
}
